package com.mt.roll.wms.job;

import cn.hutool.core.collection.CollUtil;
import com.mt.roll.core.dal.entity.CarTaskExecutionInfo;
import com.mt.roll.core.dal.service.CarTaskExecutionInfoServiceBiz;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author weihongyang
 * @Date 2021/11/18 2:29 下午
 * @Version 1.0
 */
@Component
@Slf4j
public class RgvTask {

    @Autowired
    private CarTaskExecutionInfoServiceBiz carTaskExecutionInfoServiceBiz;

//    @Scheduled(cron = "0/3 * * * * ?")
    public void carTaskExecute(){
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info(sdf.format(new Date())+"每3秒执行一次");

        //先查询rgv两台rgv是不是空闲任务的
        //根据rgv状态分配任务等

        //查询任务表中还没有执行的任务
        List<CarTaskExecutionInfo> carTaskExecutionInfoList = carTaskExecutionInfoServiceBiz.list();
        if (CollUtil.isEmpty(carTaskExecutionInfoList)) {
            for (Map.Entry<Long, List<CarTaskExecutionInfo>> entry:carTaskExecutionInfoList.stream().filter(carTaskExecutionInfo -> carTaskExecutionInfo.getStatus() == 0).collect(Collectors.groupingBy(CarTaskExecutionInfo::getEquipmentId,Collectors.toList())).entrySet()) {
                Long equipmentId = entry.getKey();
                List<CarTaskExecutionInfo> carTaskExecutionInfos = entry.getValue();
                if (equipmentId.equals(1L)) {
                    carTaskExecutionInfos.get(0);
                    //投放任务

                    //修改状态
                }
                if (equipmentId.equals(2L)) {
                    carTaskExecutionInfos.get(0);
                    //投放任务

                    //修改状态
                }
            }
        }

    }
}
